﻿/***************************************************COPYRIGHT***************************************
* Copyright (c) 2008, Whiteboard-IT
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that 
* the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following 
 * disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the distribution.
*
* Neither the name of Whiteboard-IT, LLC nor the names of its contributors may be used to endorse or promote 
 * products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
*****************************************************************************************************************/
using System;
using System.Collections.Generic;
using System.Data;
using Boo.Lang;

namespace Mite
{
	public interface IAdapter
	{
		AlterationValidation AlterationValidator { get; set; }

		/// <summary>
		/// Retrieve an enumerable of the databases that exist.
		/// </summary>
		IEnumerable<Database> GetDatabases(IDbConnection connection);

		/// <summary>
		/// Create <paramref name="table"/>.
		/// </summary>
		/// <param name="table">Table to be created.</param>
		/// <param name="connection">Connection with which to associate the command.</param>
		IDbCommand CreateTable(IDbConnection connection, Table table);

		/// <summary>
		/// Delete table from the database.
		/// </summary>
		IDbCommand DropTable(IDbConnection connection, Table table);

		/// <summary>
		/// Rename <paramref name="table"/>.
		/// </summary>
		/// <param name="table">Table to rename.</param>
		/// <param name="newName">New name of the table.</param>
		/// <param name="connection">Connection with which to associate the returned command.</param>
		/// <returns>Command to rename the table.</returns>
		IDbCommand RenameTable(IDbConnection connection, Table table, string newName);

		/// <summary>
		/// Rename <paramref name="column"/>.
		/// </summary>
		/// <param name="column">Column to rename.</param>
		/// <param name="newName">New name of <paramref name="column"/>.</param>
		/// <param name="connection">Connection with which to associate the returned command.</param>
		/// <returns>Command to rename column.</returns>
		IDbCommand RenameColumn(IDbConnection connection, Column column, string newName);

		/// <summary>
		/// Add <paramref name="column"/> to <paramref name="table"/>.
		/// </summary>
		/// <param name="table">Table to which to add <paramref name="column"/>.</param>
		/// <param name="column">Column to add.</param>
		/// <param name="connection">Connection with which to associate the returned <see cref="IDbCommand"/>.</param>
		/// <returns>Command to add <paramref name="column"/> to <paramref name="table"/>.</returns>
		IDbCommand AddColumnToTable(IDbConnection connection, Table table, Column column);

		/// <summary>
		/// Generate command to drop <paramref name="columns"/>.
		/// </summary>
		/// <param name="connection">Connection with which to associate the commands.</param>
		/// <param name="table">Table to which the columns belong.</param>
		/// <param name="columns">Columns to drop.</param>
		/// <returns>A command to drop <paramref name="columns"/>.</returns>
		IDbCommand DropColumns(IDbConnection connection, string table, params string[] columns);

		/// <summary>
		/// Alter <paramref name="column"/> to have the signature of <paramref name="newColumn"/>.
		/// The columns must have the same name.  If you wish to rename the column, please use RenameColumn instead.
		/// </summary>
		/// <param name="column">Column to alter.</param>
		/// <param name="newColumn">New column signature.</param>
		/// <param name="connection">Connection with which to associate the returned <see cref="IDbCommand"/>.</param>
		/// <returns>Command to alter column.</returns>
		/// <exception cref="InvalidOperationException">Columns must have the same name.  Use RenameColumn to rename.</exception>
		IDbCommand AlterColumn(IDbConnection connection, Column column, Column newColumn);

		/// <summary>
		/// Create the migration schema table that contains meta data about
		/// migration version in <paramref name="database"/>.
		/// If the migration schema table already exists, retrieves it and returns it.
		/// </summary>
		IDbCommand[] CreateSchemaTable(IDbConnection connection, Database database);

		Database GetDatabase(IDbConnection connection, string name);

		int GetSchemaVersion(IDbConnection connection, Database database);

		IDbCommand SetSchemaVersion(IDbConnection conn, int i);

		/// <summary>
		/// Creates a connection.  The caller is responsible for opening, closing, and disposing of the returned connection.
		/// </summary>
		IDbConnection CreateConnection(string host, string database, string username, string password,
		                               Hash options);

		/// <summary>
		/// Creates an index.
		/// </summary>
		/// <remarks>
		/// It is the responsibilty of the caller to dispose of the command returned by this method.
		/// </remarks>
		/// <param name="index">The index to create.</param>
		/// <param name="connection">Connection with which to associate the <see cref="IDbCommand"/>.</param>
		/// <returns>Command to create the index.</returns>
		IDbCommand CreateIndex(Index index, IDbConnection connection);

		/// <summary>
		/// Drops an index.
		/// </summary>
		/// <remarks>
		/// It is the responsibilty of the caller to dispose of the command returned by this method.
		/// </remarks>
		/// <param name="index">The index to drop.</param>
		/// <param name="transaction">
		/// Transaction with which to associate the <see cref="IDbCommand"/>.  
		/// The transaction must be associated with a connection.
		/// </param>
		/// <returns>Command to drop the index.</returns>
		IDbCommand DropIndex(Index index, IDbTransaction transaction);

		/// <summary>
		/// Drops an index.
		/// It is the responsibility of the caller to dispose of the <see cref="IDbCommand"/> returned by this method.
		/// </summary>
		/// <param name="index">The index to drop.</param>
		/// <param name="connection">Connection with which to associate the <see cref="IDbCommand"/>.</param>
		/// <returns>Command to drop the index.</returns>
		IDbCommand DropIndex(Index index, IDbConnection connection);

		/// <summary>
		/// Add foreign key constraint to <paramref name="table"/>.
		/// </summary>
		/// <param name="connection">Connection with which to associate the <see cref="IDbCommand"/> returned.</param>
		/// <param name="table">Name of the table to constrain.</param>
		/// <param name="refernceTable">Reference table for the foreign key constraint.</param>
		/// <param name="name">Name of the constraint.</param>
		/// <param name="referenceMap">Map of names </param>
		/// <returns>Command to add foreign key.</returns>
		IDbCommand AddForeignKey(IDbConnection connection, string table, string refernceTable, string name, params KeyValuePair<string, string>[] referenceMap);

		/// <summary>
		/// Remove foreign key constraint.
		/// </summary>
		/// <returns>Command to remove <paramref name="name"/>.</returns>
		IDbCommand DropForeignKey(IDbConnection connection, string table, string name);
	}

	public interface IDaoFactory
	{
		IAdapter Create(string connectionString);
	}
}